<?php
namespace  app\admin\controller;
use think\Db;
class Partner extends Base
{
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(session ('area')){
                $map['par_qu_id'] = ['in',session('area')];
            }
            if(isset($role)&&$role != ""){
                $map['par_status'] = $role;
            }
            if(isset($key)&&$key!="")
            {
                $map['user_username|district'] = ['like',"%" . $key . "%"];
            }
            $map['par_class_level'] = ['not in',[1,2]];
            $map['par_cancel'] = 0;
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('partner p')
                ->join('u_user u','u.user_id = p.par_user_id','left')
                ->join('area a','a.district_id = p.par_qu_id','left')
                ->join('admin m','m.area = p.par_qu_id','left')
                ->where($map)
                ->count();
            $lists = Db::name('partner p')
                ->join('u_user u','u.user_id = p.par_user_id','left')
                ->join('area a','a.district_id = p.par_qu_id','left')
                ->join('admin m','m.area = p.par_qu_id','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order ('par_sort desc,par_add_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $admin = Db::name('admin')->where('id',session('uid'))->find();
        return $this->fetch("partner/index",['admin'=>$admin]);
    }


    public function partnerNum(){
        extract (input());
        if($number <= 0){
            return json(['code'=>100,'msg'=>'非零正整数']);
        }
        $res = Db::name('admin')->where('id',session('uid'))->update (['partner'=>$number,'update_time'=>time()]);
        if($res){
            return json(['code'=>200,'msg'=>'编辑成功']);
        }else{
            return json(['code'=>100,'msg'=>'编辑失败']);
        }
    }


    public function createAccount(){
        if(request ()->isPost ()){
            extract (input());
            for($i = 0;$i < $number;$i++){
                $param[] = [
                    'par_qu_id'=>$area,
                    'par_edit_time'=>date('Y-m-d H:i:s'),
                    'par_create_time'=>date('Y-m-d H:i:s')
                ];
            }
            $res = Db::name('partner')->insertAll($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        $area = Db::name('admin')->where(['type'=>3])->select ();
        return $this->fetch ('partner/create_account',['area'=>$area]);
    }


    public function partnerAdd(){
        extract (input());
        if(request ()->isPost ()){
            Db::startTrans ();
            try{
                $param = [
                    'par_user_id'=>$user,
                    'par_status'=>2,
                    'par_edit_time'=>date('Y-m-d H:i:s'),
                    'par_add_time'=>date('Y-m-d H:i:s'),
                    'par_percent'=>$now
                ];
                Db::name('partner')->where('par_id',$id)->update($param);
                if(isset($par_id) && !empty($par_id)){
                    foreach($par_id as $key=>$vo){
                        Db::name('partner')->where ('par_id',$vo)->update (['par_percent'=>$percent[$key],'par_edit_time'=>date('Y-m-d H:i:s')]);
                    }
                }
                Db::name('u_user')->where('user_id',$user)->update(['user_level'=>2,'user_edit_time'=>date('Y-m-d H:i:s')]);
                Db::commit ();
                return json(['code'=>200,'msg'=>"绑定成功"]);
            }catch (\Exception $e){
                Db::rollback ();
                return json(['code'=>100,'msg'=>'绑定失败']);
            }
        }
        $area = Db::name('partner')->where('par_id',$id)->value('par_qu_id');
        $user = Db::name('u_user')->where('user_area',$area)->select ();
        $par = Db::name('partner p')
            ->join('u_user u','u.user_id = p.par_user_id','left')
            ->where(['par_qu_id'=>$area,'par_status'=>2,'par_class_level'=>3,'par_cancel'=>0])
            ->select ();
        return $this->fetch ('partner/partner_add',['user'=>$user,'par_id'=>$id,'par'=>$par]);
    }

    public function partnerAdd1(){
        extract (input());
        if(request ()->isPost ()){
            Db::startTrans ();
            try{
                if(isset($par_id) && !empty($par_id)){
                    foreach($par_id as $key=>$vo){
                        Db::name('partner')->where ('par_id',$vo)->update (['par_percent'=>$percent[$key],'par_edit_time'=>date('Y-m-d H:i:s')]);
                    }
                }
                Db::commit ();
                return json(['code'=>200,'msg'=>"编辑成功"]);
            }catch (\Exception $e){
                Db::rollback ();
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $area = Db::name('partner')->where('par_id',$id)->value('par_qu_id');
        $user = Db::name('u_user')->where('user_area',$area)->select ();
        $par = Db::name('partner p')
            ->join('u_user u','u.user_id = p.par_user_id','left')
            ->where(['par_qu_id'=>$area,'par_status'=>2,'par_class_level'=>3,'par_cancel'=>0])
            ->select ();
        return $this->fetch ('partner/partner_add1',['user'=>$user,'par_id'=>$id,'par'=>$par]);
    }


    public function partnerEdit(){
        extract (input());
        if(request()->isPost ()) {
            Db::startTrans ();
            try{
                foreach($par_id as $key=>$vo){
                    if($percent[$key] == 0){
                        $user = Db::name('partner')->where('par_id',$vo)->value ('par_user_id');
                        Db::name('partner')->where('par_id',$vo)->update (['par_user_id'=>null,'par_status'=>1,'par_add_time'=>null,'par_edit_time'=>date("Y-m-d H:i:s"),'par_percent'=>'']);
                        $data = Db::name('partner')->where('par_user_id',$user)->select ();
                        if(empty($data)){
                            Db::name('u_user')->where('user_id',$user)->update(['user_level'=>1,'user_edit_time'=>date('Y-m-d H:i:s')]);
                        }
                    }else{
                        Db::name('partner')->where ('par_id',$vo)->update (['par_percent'=>$percent[$key],'par_edit_time'=>date('Y-m-d H:i:s')]);
                    }
                }
                Db::commit ();
                return json (['code' => 200 , 'msg' => "解绑成功"]);
            }catch (\Exception $e){
                Db::rollback ();
                return json (['code' => 100 , 'msg' => '解绑失败']);
            }
        }
        $area = Db::name('partner')->where('par_id',$id)->value('par_qu_id');
        $par = Db::name('partner p')
            ->join('u_user u','u.user_id = p.par_user_id','left')
            ->where(['par_qu_id'=>$area,'par_status'=>2,'par_class_level'=>3,'par_cancel'=>0])
            ->select ();
        return $this->fetch ('partner/partner_edit',['par_id'=>$id,'par'=>$par]);
    }


    public function partnerDel(){
        extract (input());
        $user = Db::name('partner')->where('par_id',$id)->value ('par_user_id');
        $res = Db::name('partner')->where('par_id',$id)->delete ();
        if($res){
            $data = Db::name('partner')->where('par_user_id',$user)->select ();
            if(empty($data)){
                Db::name('u_user')->where('user_id',$user)->update(['user_level'=>1,'user_edit_time'=>date('Y-m-d H:i:s')]);
            }
            return json(['code'=>200,'msg'=>"删除成功"]);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }


    public function money(){
        extract (input());
        if(request()->isAjax ()){
            $map['m_user_id'] = session('partner_id');
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('earnings e')
                ->join('u_user u','e.m_user_id = u.user_id','left')
                ->where($map)
                ->count();
            $lists = Db::name('earnings e')
                ->join('u_user u','e.m_user_id = u.user_id','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order ('m_add_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        session('partner_id',$id);
        return $this->fetch ('partner/money');
    }

    public function addSort(){
        extract (input());
        $res = Db::name('partner')
            ->where('par_id',$id)
            ->update(['par_sort'=>$num,'par_edit_time'=>date('Y-m-d H:i:s')]);
        if($res){
            return json(['code'=>200,'msg'=>'编辑成功']);
        }else{
            return json(['code'=>100,'msg'=>'编辑失败']);
        }
    }
}